﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;

namespace QuanLyKTX
{
    using DAL;
    using System.Linq;
    using Utilities;
    public partial class frm_ReportEndContact : Telerik.WinControls.UI.RadForm
    {
        public tbl_SinhVien SinhVien { get; set; }
        public tbl_HopDong HopDong { get; set; }

        public frm_ReportEndContact(tbl_SinhVien sinhVien, tbl_HopDong hopDong)
        {
            InitializeComponent();
            SinhVien = sinhVien;
            HopDong = hopDong;
        }

        private void frm_ReportEndContact_Load(object sender, EventArgs e)
        {

            string tenKhoa = "";
            string tenTruong = "";
            using (var db = new QuanLyKTXDataContext())
            {
                var khoa = (from l in db.tbl_Lops join k in db.tbl_Khoas on l.MaKhoa equals k.MaKhoa where l.MaLop == SinhVien.MaLop select k.TenKhoa).FirstOrDefault();
                tenKhoa = khoa;

                var truong = (from p in db.tbl_Truongs
                              join q in db.tbl_SinhViens on p.MaTruong equals q.MaTruong
                              where p.MaTruong == SinhVien.MaTruong
                              select p).FirstOrDefault();
                tenTruong = truong.TenTruong.Trim();
            }

            System.Data.DataRow dRow = dataSetBienBanThanhLyHD.Tables[0].NewRow();
            dRow["MaSinhVien"] = SinhVien.MaSinhVien;
            dRow["MaLop"] = SinhVien.MaLop;
            dRow["Khoa"] = tenKhoa;
            dRow["HoTen"] = String.Format("{0} {1}", SinhVien.Ho, SinhVien.Ten);
            dRow["SoCMND"] = SinhVien.SoCMND;
            dRow["NoiCap"] = SinhVien.NoiCap;
            if (SinhVien.NgayCap != null)
                dRow["NgayCap"] = SinhVien.NgayCap.Value.Date;
            dRow["DiaChi"] = SinhVien.DiaChi;
            dRow["SoDienThoai"] = SinhVien.SoDienThoai;
            dRow["SoDienThoaiCaNhan"] = SinhVien.SoDienThoaiCN;
            dRow["NgayBatDauHD"] = HopDong.NgayBatDauHD;
            dRow["PhiBanDau"] = SinhVien.MaTruong == "VLU" ? HopDong.PhiBanDauVL : HopDong.PhiBanDau;
            dRow["PhiSauThanhLy"] = HopDong.PhiSauThanhLy;
            decimal soTienConLai = SinhVien.MaTruong == "VLU" ? Convert.ToDecimal(HopDong.PhiBanDauVL - HopDong.PhiSauThanhLy) : Convert.ToDecimal(HopDong.PhiBanDau - HopDong.PhiSauThanhLy);

            dRow["SoTienConLai"] = soTienConLai;
            dRow["NgayThanhLy"] = HopDong.NgayThanhLyHD;


            dRow["ThanhChu"] = new CNumToString().convertStart(String.Format("{0:0.##}", soTienConLai)) + " )";
            dRow["SoHopDong"] = String.Format("{0}/{1}", HopDong.SoHopDong, HopDong.HauTo);
            dRow["NgayLapHD"] = HopDong.NgayKyHD.Value;

            dataSetBienBanThanhLyHD.Tables[0].Rows.Add(dRow);

            reportViewer_BBThanhLyHD.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet_BBThanhLy", dataSetBienBanThanhLyHD.Tables[0]));
            reportViewer_BBThanhLyHD.SetDisplayMode(Microsoft.Reporting.WinForms.DisplayMode.PrintLayout);
            reportViewer_BBThanhLyHD.ZoomMode = Microsoft.Reporting.WinForms.ZoomMode.PageWidth;

            this.reportViewer_BBThanhLyHD.RefreshReport();
        }
    }
}
